//
//  FQDLabTitleImageView.m
//  FakeQDaily
//
//  Created by mewxiv on 8/15/16.
//  Copyright © 2016 Xensius. All rights reserved.
//

#import "FQDLabTitleImageView.h"
#import "FQDRestModel.h"

#import "Masonry.h"
#import "UIImageView+WebCache.h"


@interface FQDLabTitleImageView ()

// 背景图
@property (nonatomic, strong) UIImageView *backgroundImageView;
// 夹在背景图和上层图标中间的图片层
@property (nonatomic, strong) UIImageView *middleImageView;
// 类别图标
@property (nonatomic, strong) UIImageView *categoryIconView;
// 位于左上角的图片
@property (nonatomic, strong) UIImageView *topLeftImageView;

@end

@implementation FQDLabTitleImageView


- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self prepareUI];
        [self prepareConstraints];
    }
    return self;
}

- (void)prepareUI
{
    UIImage *bgImage = [UIImage imageNamed:@"labFeedBgImage"];
    _backgroundImageView = [[UIImageView alloc] initWithImage:bgImage];
    _backgroundImageView.contentMode = UIViewContentModeScaleAspectFill;
    [self addSubview:_backgroundImageView];
    
    _middleImageView = [[UIImageView alloc] init];
    _middleImageView.contentMode = UIViewContentModeScaleAspectFill;
    [self addSubview:_middleImageView];
    
    UIImage *iconImage = [UIImage imageNamed:@"mySubscribeIcon"];
    _categoryIconView = [[UIImageView alloc] initWithImage:iconImage];
    _categoryIconView.contentMode = UIViewContentModeScaleAspectFill;
    [_categoryIconView setClipsToBounds:YES];
    [self addSubview:_categoryIconView];
    
    _joinCountView = [[FQDLabJoinCountView alloc] init];
    _joinCountView.contentMode = UIViewContentModeScaleAspectFill;
    [self addSubview:_joinCountView];
    
    _topLeftImageView = [[UIImageView alloc] init];
    _topLeftImageView.contentMode = UIViewContentModeScaleAspectFill;
    [_topLeftImageView setClipsToBounds:YES];
    //    _topLeftImageView.hidden = YES;
    [self addSubview:_topLeftImageView];
}

- (void)prepareConstraints
{
    WeakSelf;
    
    [_backgroundImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(weakSelf);
    }];
    
    [_middleImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(weakSelf.backgroundImageView);
    }];
    
    [_categoryIconView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf).offset(10);
        make.bottom.equalTo(weakSelf).offset(-10);
        make.size.equalTo(@35);
    }];
    
    [_joinCountView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf).offset(11);
        make.right.equalTo(weakSelf).offset(-11);
        make.width.equalTo(@57);
        make.height.equalTo(@48);
    }];
    
    [_topLeftImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf);
        make.left.equalTo(weakSelf);
        make.size.equalTo(@65);
    }];
}

#pragma Getters and Setters
- (void)setPost:(FQDPost *)post
{
    _post = post;
    
    [self.backgroundImageView sd_setImageWithURL:[NSURL URLWithString:post.image]];
    
    [self.categoryIconView sd_setImageWithURL:[NSURL URLWithString:post.category.image_lab]];
    
    // TODO: 修改判断是否参加的逻辑
    BOOL isJoined = YES;
    if (isJoined) {
        [self.joinCountView showJoinedNum:post.record_count];
    }
    else {
        [self.joinCountView showJoinIn];
    }
}


@end
